#include<bits/stdc++.h>
using namespace std;
#define int long long
#define endl '\n'

signed main() {
    ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);
    int n; cin >> n;
    while (n--) {
        string s; cin >> s;
        map<char, int> mp;
        int p = 0, t = 0;
        for (int i = 0; i < s.size(); i++) {
            mp[s[i]]++;
            if (s[i] == 'P') p = i;
            if (s[i] == 'T') t = i;
        }
        if (mp.size() == 3 && mp['P'] == 1 && mp['T'] == 1
            && mp['A'] != 0 && t - p > 1
            && p * (t - p - 1) == s.size() - 1 - t)
            cout << "YES" << endl;
        else cout << "NO" << endl;
    }
    return 0;
}